package cn.com.self.apis.portal.controller;

import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.portal.entity.DatatypeGroupEntity;
import cn.com.self.apis.portal.service.intf.IDatatypeGroupService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.portal.util.StringUtils;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.Constants;
import cn.com.selfsoft.smartcity.commons.exception.OutOfBusinessException;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import cn.com.selfsoft.smartcity.core.controller.GenericControllerImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 数据类型组 前端控制器
 */
@RestController
@Api(tags = "字典数据类型信息类")
public class DatatypeGroupController extends GenericControllerImpl<DatatypeGroupEntity, String> {

    @Autowired
    private IDatatypeGroupService datatypeGroupService;

    /**
     * 获取字典类型数目
     *
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatypeGroup"}, method = RequestMethod.GET, params = {Constants.DEFAULT_MARK_PARAMETER + "=count"})
    @ApiOperation(value = "获取字典类型数目")
    public ResponseEntity count(HttpServletRequest request) {
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark","access_token");
        Long total = this.datatypeGroupService.count(queryMap);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(total));
    }

    /**
     * 分页获取字典类型列表
     *
     * @param request
     * @param page
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatypeGroup"}, method = RequestMethod.GET, params = {Constants.DEFAULT_MARK_PARAMETER + "=page"})
    @ApiOperation(value = "分页获取字典类型列表")
    public ResponseEntity page(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","timestamp","access_token");
        ICustomPage<DatatypeGroupEntity> pageData = this.datatypeGroupService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }

    /**
     * 获取字典类型列表
     *
     * @param request
     * @param sort
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatypeGroup"}, method = RequestMethod.GET, params = {Constants.DEFAULT_MARK_PARAMETER + "=list"})
    @ApiOperation(value = "获取字典类型列表")
    public ResponseEntity list(HttpServletRequest request, Sort sort) {
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark","timestamp","access_token");
        List<DatatypeGroupEntity> list = this.datatypeGroupService.list(queryMap, sort);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(list));
    }

    /**
     * 获取字典类型
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatypeGroup/load/{id}"}, method = RequestMethod.GET)
    @ApiOperation(value = "获取字典类型")
    public ResponseEntity load(@PathVariable("id") String id, HttpServletRequest request) {
        DatatypeGroupEntity datatypeGroupEntity = this.datatypeGroupService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(datatypeGroupEntity));
    }

    /**
     * 删除字典类型
     *
     * @param id
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatypeGroup/delete/{id}"}, method = RequestMethod.DELETE)
    @ApiOperation(value = "删除字典类型")
    public ResponseEntity delete(@PathVariable("id") String id, HttpServletRequest request) {
        this.datatypeGroupService.delete(Long.parseLong(id));
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    /**
     * 创建字典类型
     *
     * @param entity
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatypeGroup/create"}, method = RequestMethod.POST)
    @ApiOperation(value = "创建字典类型")
    public ResponseEntity create(@RequestBody DatatypeGroupEntity entity, HttpServletRequest request) {
        Long total  =this.datatypeGroupService.countByGrouptype(entity.getGrouptype());
        if(total !=null && total>0){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(null));
        }
        DatatypeGroupEntity datatypeGroupEntity = this.datatypeGroupService.create(entity);
        //同步redis字典缓存
        this.datatypeGroupService.updateRedis(datatypeGroupEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(datatypeGroupEntity));
    }

    /**
     * 修改字典类型
     *
     * @param id
     * @param entity
     * @param request
     * @return
     */
    @Override
    @RequestMapping(value = {"/datatypeGroup/update/{id}"}, method = RequestMethod.POST)
    @ApiOperation(value = "修改字典类型")
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody DatatypeGroupEntity entity, HttpServletRequest request) {
        DatatypeGroupEntity datatypeGroupEntity = this.datatypeGroupService.load(id);
        if (datatypeGroupEntity != null) {
            CopyUtils.copyProperties(entity, datatypeGroupEntity);
            this.datatypeGroupService.update(datatypeGroupEntity);
            //同步redis字典缓存
            this.datatypeGroupService.updateRedis(datatypeGroupEntity);
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(datatypeGroupEntity));
        } else {
            throw new OutOfBusinessException("id不存在");

        }
    }


}
